//
//  XIShape.m
//  XI Engine
//
//  Created by Balazs Faludi on 31.05.09.
//  Copyright 2009 Balazs Faludi. All rights reserved.
//

#import "XIShape.h"
#import "XIEngine.h"

@implementation XIShape

@synthesize alpha, size, center, angle;
//@synthesize _centerX, _centerY. _sizeWidth, _sizeHeight;
@synthesize physicsEnabled;
#if TARGET_OS_IPHONE
@synthesize shape, body;
#endif

#pragma mark -
#pragma mark Initializer

- (id)init {
    if ((self = [super init])) {
		alpha = 1.0;
		size = CGSizeZero;
		center = CGPointZero;
		angle = 0.0;
		physicsEnabled = NO;
#if TARGET_OS_IPHONE
		body = nil;
		shape = nil;
#endif
    }
    return self;
}

- (void)dealloc {
#if TARGET_OS_IPHONE
	cpBodyFree(body);
	cpShapeFree(shape);
#endif
    [super dealloc];
}

#pragma mark -
#pragma mark Notifications

- (void)wasAddedToLayer:(XILayer *)newLayer
{
#if TARGET_OS_IPHONE
	[self updateBodyAndShape];
#endif
}

#pragma mark -
#pragma mark Setters

- (void)setCenterFromXil:(NSDictionary *)dict
{
	center.x = [[dict objectForKey:@"x"] floatValue];
	center.y = [[dict objectForKey:@"y"] floatValue];
#if TARGET_OS_IPHONE
	[self updateBodyAndShape];
#endif
}

- (void)setSizeFromXil:(NSDictionary *)dict
{
	size.width = [[dict objectForKey:@"width"] floatValue];
	size.height = [[dict objectForKey:@"height"] floatValue];
#if TARGET_OS_IPHONE
	[self updateBodyAndShape];
#endif
}

#pragma mark -
#pragma mark Save

- (NSMutableDictionary *)saveStateToDictionary
{
	NSMutableDictionary *state = [super saveStateToDictionary];
	NSDictionary *centerDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:center.x], @"x", 
																		  [NSNumber numberWithFloat:center.y], @"y", nil];
	NSDictionary *sizeDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:size.width], @"width", 
																		[NSNumber numberWithFloat:size.height], @"height", nil];
	[state setObject:[NSNumber numberWithFloat:alpha] forKey:@"alpha"];
	[state setObject:[NSNumber numberWithFloat:angle] forKey:@"angle"];
	[state setObject:[NSNumber numberWithBool:physicsEnabled] forKey:@"physicsEnabled"];
	[state setObject:centerDict forKey:@"center"];
	[state setObject:sizeDict forKey:@"size"];
	return state;
}

#pragma mark -
#pragma mark Physics

#if TARGET_OS_IPHONE
- (void)shapeWasUpdated
{
	center = CGPointMake(body->p.x, body->p.y);
	angle = body->a;
}

- (cpBody *)createBody
{
	cpBody *myBody = cpBodyNew(150, 300.0);  	// Create our body with 100 mass and infinite moment.
	cpBodySetMass(myBody, 10);
	myBody->p = cpv(center.x, center.y);  // Set the initial position.
	myBody->a = angle;	// Set the initial angle.
	return myBody;
}

- (cpShape *)createShape
{
	cpVect verts[] = {cpv(-size.width / 2.0, size.height / 2.0), cpv(size.width / 2.0, size.height / 2.0), 
					  cpv(size.width / 2.0, -size.height / 2.0), cpv(-size.width / 2.0, -size.height / 2.0)};
	cpShape *myShape = cpPolyShapeNew(body, 4, verts, cpvzero);  // Create our shape associated with the body.
	myShape->e = kXIShapeElasticityDefault; // Elasticity  
	myShape->u = kXIShapeFrictionDefault; // Friction  
	myShape->data = self; // Associate with out ball's UIImageView  
	myShape->collision_type = 1; // Collisions are grouped by types
	return myShape;
}

- (void)updateBodyAndShape
{
	if(body){cpBodyFree(body);}
	body = [self createBody];
	if(layer && layer.space)
	{
		cpSpaceAddBody(layer.space, body);
	}
	
	if(shape){cpShapeFree(shape);}
	shape = [self createShape];
	if(layer && layer.space)
	{
		cpSpaceAddShape(layer.space, shape);
	}	
}

- (void)enablePhysics
{
	if(physicsEnabled){return;}
	physicsEnabled = YES;

	[self updateBodyAndShape];
}

- (void)disablePhysics
{
	if(!physicsEnabled){return;}
	physicsEnabled = NO;
	
	cpBodyFree(body);
	cpShapeFree(shape);
}

- (void)setPhysicsEnabled:(BOOL)enabled
{
	enabled ? [self enablePhysics] : [self disablePhysics];
}

-(cpBody*)body
{
	return body;
}

- (cpShape*)shape
{
	return shape;
}
#endif

#pragma mark -
#pragma mark Loop

#if TARGET_OS_IPHONE
- (void)update
{
	
}
#endif

- (void)draw
{
/*	glPushMatrix();
	glTranslatef(center.x, center.y, 0);
	glRotatef(angle * 180.0 / M_PI, 0.0, 0.0, 1.0);
#ifdef kXIDisplayShapeBounds
	//[XIBrush drawCircleAtPoint:CGPointZero withRadius:20.0 withColor:[XIColor redColor]];
	[XIBrush drawRect:CGRectMake(-size.width / 2.0, -size.height / 2.0, size.width, size.height) withColor:[XIColor redColor]];
	[XIBrush drawCrossAtPoint:CGPointZero withColor:[XIColor blueColor]];
#endif
	glPopMatrix();*/
}

#if TARGET_OS_MAC
- (void)drawForSceneBuilder
{	
	[self draw];
	
	glPushMatrix();
	glTranslatef(center.x, center.y, 0);
	glRotatef(angle * 180.0 / M_PI, 0.0, 0.0, 1.0);

	[XIBrush drawRect:CGRectMake(-size.width / 2.0, -size.height / 2.0, size.width, size.height) withColor:[XIColor redColor]];
	[XIBrush drawCrossAtPoint:CGPointZero withColor:[XIColor blueColor]];

	glPopMatrix();
}
#endif



@end
